package by.semerikov.action;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import by.semerikov.constants.Results;
import by.semerikov.exceptions.ConnectorException;
import by.semerikov.exceptions.DAOException;
import by.semerikov.model.Person;

public class LanguageSelectAction extends SecuredAction {
	@Override
	public ActionForward executedInternal(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response, Person user) throws DAOException,
			ConnectorException {

		String lang = request.getParameter("lang");
		if (!"en".equals(lang.toLowerCase())
				&& !"ru".equals(lang.toLowerCase())) {

			lang = "en"; // by default
		}

		Locale locale = new Locale(lang);
		String forwardTo = request.getHeader("Referer");

		request.getSession(true).setAttribute(Globals.LOCALE_KEY, locale);
		response.setLocale(locale);

		if (forwardTo.isEmpty()) {
			forwardTo = Results.SUCCESS;
		}

		return new ActionForward(forwardTo, true);
	}
}
